package com.ayoka.widget;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.ayoka.R;
import com.jcp.baselibrary.dialog.BaseDialogBuilder;
import com.jcp.baselibrary.dialog.BaseDialogFragment;
import com.jcp.baselibrary.utils.UIUtils;

/**
 * Created by jinchunping on 2019/1/23.
 */

public class OneButtonDialog extends BaseDialogFragment {

    public static OneButtonDialog newInstance(Builder builder) {
        OneButtonDialog dialog = new OneButtonDialog();
        Bundle bundle = getArgumentBundle(builder);
        bundle.putString("DialogTitle", builder.title);
        bundle.putString("DialogMessage", builder.message);
        bundle.putString("PositiveText", builder.positiveText);
        dialog.setArguments(bundle);
        return dialog;
    }

    @Override
    protected void convertView(@Nullable View container, final BaseDialogFragment dialogFragment) {
        TextView tvDialogTitle = container.findViewById(R.id.tv_dialog_title);
        String dialogTitle = getArguments().getString("DialogTitle");
        if (TextUtils.isEmpty(dialogTitle)){
            tvDialogTitle.setVisibility(View.GONE);
        } else {
            tvDialogTitle.setVisibility(View.VISIBLE);
            tvDialogTitle.setText(getArguments().getString("DialogTitle"));
        }

        TextView tvDialogMessage = container.findViewById(R.id.tv_dialog_message);
        String dialogMessage = getArguments().getString("DialogMessage");
        tvDialogMessage.setText(dialogMessage);

        Button btnPositive = container.findViewById(R.id.btn_positive);
        String positiveText = getArguments().getString("PositiveText");
        if (TextUtils.isEmpty(positiveText)){
            positiveText = UIUtils.getString(R.string.confirm);
        }
        btnPositive.setText(positiveText);
        btnPositive.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismissAllowingStateLoss();
                if (mDialogResultListener != null){
                    mDialogResultListener.result(OneButtonDialog.this, true);
                }
            }
        });
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_confirm_one_button;
    }

    public static class Builder extends BaseDialogBuilder<Builder, OneButtonDialog> {

        private String title ,message, positiveText;

        public Builder setTitle(String title) {
            this.title = title;
            return this;
        }

        public Builder setMessage(String message) {
            this.message = message;
            return this;
        }

        public Builder setPositiveText(String positiveText) {
            this.positiveText = positiveText;
            return this;
        }

        @Override
        public OneButtonDialog build() {
            return OneButtonDialog.newInstance(this);
        }
    }

}